package com.ruoyi.web.controller.data;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OpinionSourceFile;
import com.ruoyi.system.service.IOpinionSourceFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 导入舆情文件Controller
 * 
 * @author ruoyi
 * @date 2021-07-19
 */
@RestController
@RequestMapping("/system/opsourcefile")
public class OpinionSourceFileController extends BaseController
{
    @Autowired
    private IOpinionSourceFileService opinionSourceFileService;

    /**
     * 查询导入舆情文件列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(OpinionSourceFile opinionSourceFile)
    {

        startPage();
        opinionSourceFile.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<OpinionSourceFile> list = opinionSourceFileService.selectOpinionSourceFileList(opinionSourceFile);
        list.forEach(sourceFile -> {
            if (sourceFile.getExtractText()!=null&&sourceFile.getExtractText().length() > 255) {
                sourceFile.setExtractText(sourceFile.getExtractText().substring(0, 255));
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出导入舆情文件列表
     */
    @Log(title = "导入舆情文件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OpinionSourceFile opinionSourceFile)
    {
        opinionSourceFile.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<OpinionSourceFile> list = opinionSourceFileService.selectOpinionSourceFileList(opinionSourceFile);
        ExcelUtil<OpinionSourceFile> util = new ExcelUtil<>(OpinionSourceFile.class);
        return util.exportExcel(list, "导入舆情文件数据");
    }

    /**
     * 获取导入舆情文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(opinionSourceFileService.selectOpinionSourceFileById(id));
    }

    /**
     * 新增导入舆情文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "导入舆情文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OpinionSourceFile opinionSourceFile)
    {
        opinionSourceFile.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        opinionSourceFile.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        opinionSourceFile.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        return toAjax(opinionSourceFileService.insertOpinionSourceFile(opinionSourceFile));
    }

    /**
     * 修改导入舆情文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "导入舆情文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OpinionSourceFile opinionSourceFile)
    {
        return toAjax(opinionSourceFileService.updateOpinionSourceFile(opinionSourceFile));
    }

    /**
     * 删除导入舆情文件
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "导入舆情文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(opinionSourceFileService.deleteOpinionSourceFileByIds(ids));
    }
}
